//
//  SliderImageView.swift
//  NetMusic
//
//  Created by PushMeBaby on 16/2/9.
//  Copyright © 2016年 CoderC. All rights reserved.
//

import UIKit

class SliderImageView: UIView, UICollectionViewDataSource , UICollectionViewDelegate ,UIScrollViewDelegate{

    
    let collectionViewLayout = UICollectionViewFlowLayout()
    
    
    let sliderImageView = UICollectionView(frame: CGRectZero, collectionViewLayout: UICollectionViewLayout())
    
    let pageView = UIPageControl()
    
    
    var timer = NSTimer()
    
    
    let imageArray = ["cm2_discover_banner_cn" ,  "cm2_discover_banner_jp" , "cm2_discover_banner_kr" , "cm2_discover_banner_us"]
    
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        
        
        sliderImageView.registerClass(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "Cell")
        sliderImageView.delegate = self
        sliderImageView.dataSource = self
        
        collectionViewLayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        collectionViewLayout.minimumInteritemSpacing = 0
        collectionViewLayout.minimumLineSpacing = 0
        sliderImageView.setCollectionViewLayout(collectionViewLayout, animated: true)
        sliderImageView.showsHorizontalScrollIndicator = false
        sliderImageView.bounces = false
        sliderImageView.pagingEnabled = true


        self.addSubview(sliderImageView)

        pageView.currentPageIndicatorTintColor = UIColor.blackColor()
        pageView.numberOfPages = imageArray.count
        self.addSubview(pageView)
        
        let time = NSTimer.init(timeInterval: 0.5, target: self, selector: "start:", userInfo: nil, repeats: true)
        timer = time

        timer.fire()
        
        
        let run = NSRunLoop()
        run.addTimer(timer, forMode: NSDefaultRunLoopMode)
//        run.finalize()

        
    }
    
    func start(time: NSTimer) {
        print(1111)
    }

    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        sliderImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self)
            make.top.equalTo(self)
            make.right.equalTo(self)
            make.bottom.equalTo(self)
        }
        pageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self.snp_bottom).offset(-10)
        }
        
        
        let indexPath = NSIndexPath.init(forRow: 0, inSection: 555)
        
        let scrollPosition = UICollectionViewScrollPosition()
        
        sliderImageView.scrollToItemAtIndexPath(indexPath, atScrollPosition: scrollPosition, animated: false)
        
        
    }
    
    func collectionView(collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, sizeForItemAtIndexPath indexPath: NSIndexPath!) -> CGSize{
        
        let size = CGSizeMake(OSScreenW, 118)
        
        return size
        
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        print("collectionView")
        
    }
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 999
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageArray.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
   
        let imageName = imageArray[indexPath.row]
        
        let imageView = UIImageView(image: UIImage(named: imageName))
        imageView.frame = cell.contentView.bounds
        cell.contentView.addSubview(imageView)
        
        return cell
    }
    
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        let intPage =  scrollView.contentOffset.x / scrollView.frame.width % CGFloat( imageArray.count)

        pageView.currentPage = Int( intPage )
        
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
